package com.quran.viewcontrollers;


import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;

import com.bappi.utils.Constants;
import com.bappi.utils.Utils;
import com.custom.tab.AbstractTabRootManager;
import com.custom.tab.AbstractViewController;
import com.markupartist.android.widget.ActionBar;
import com.tos.quran.R;

public class Tab4Controller extends AbstractViewController {

	private ActionBar actionBar;
	private boolean nativeSpeakingEnabled;
	private Button buttonNativeSpeaking;

	public Tab4Controller(AbstractTabRootManager tabRootManager) {
		super(tabRootManager, R.layout.tab4_view);
		try {
			
			nativeSpeakingEnabled = getActivity().getSharedPreferences().getBoolean(Constants.KEY_NATIVE_SPEAKING_ENABLE, false);
			actionBar = (ActionBar) findViewById(R.id.actionbar);
			buttonNativeSpeaking = (Button)findViewById(R.id.button_enable_native_speaking);
			actionBar.setTitle(R.string.text_tab4);
			findViewById(R.id.button_select_translation).setOnClickListener(
					new View.OnClickListener() {
						public void onClick(View arg0) {
							showrResetTranslation();
						}
					});
			if(nativeSpeakingEnabled){
				buttonNativeSpeaking.setText(getString(R.string.disable_native_speak));
			}else{
				buttonNativeSpeaking.setText(getString(R.string.enable_native_speak));
			}
			buttonNativeSpeaking.setOnClickListener(new View.OnClickListener() {
				
				public void onClick(View arg0) {
					handleNativeSpeaking();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void handleNativeSpeaking(){
		try {
			nativeSpeakingEnabled= !nativeSpeakingEnabled;
			
			getActivity().getSharedPreferences().edit().putBoolean(Constants.KEY_NATIVE_SPEAKING_ENABLE, nativeSpeakingEnabled).commit();
			if(nativeSpeakingEnabled){
				buttonNativeSpeaking.setText(getString(R.string.disable_native_speak));
			}else{
				buttonNativeSpeaking.setText(getString(R.string.enable_native_speak));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void showrResetTranslation() {
		try {
			Utils.showAlertDialogYesNo(getActivity(),
					getString(R.string.select_translation_conf),
					new DialogInterface.OnClickListener() {
						public void onClick(final DialogInterface dialog,
								final int id) {
							resetTranslation();
						}
					}, new DialogInterface.OnClickListener() {
						public void onClick(final DialogInterface dialog,
								final int id) {

						}
					});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void resetTranslation() {
		try {
			getActivity().getSharedPreferences().edit()
					.putString(Constants.KEY_SELECTED_TRANS_CODE, null)
					.commit();
//			getActivity().getSharedPreferences()
//					.edit()
//					.putLong(Constants.KEY_RESET_LANGUAGES_CODE,
//							System.currentTimeMillis()).commit();
//			TabsController.getTabsController().changeTab(0);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
